﻿using System;
using System.Windows.Input;
using RSSFeedReader.Commands;
using System.Diagnostics;

namespace RSSFeedReader.ViewModels
{
    /// <summary>
    /// Class to rename a <see cref="RSSFeedReader.ViewModels.ChannelViewModel"/>.
    /// </summary>
    public class RenameChannelViewModel : ViewModelBase
    {
        #region Fields
        DelegateCommand<string> _OKCommand;
        ChannelViewModel _channelViewModel;
        #endregion

        #region Constructor

        /// <summary>
        /// Initialize a new instance of the <see cref="RSSFeedReader.ViewModels.RenameChannelViewModel"/> class.
        /// </summary>
        public RenameChannelViewModel() { }

        /// <summary>
        /// Initialize a new instance of the <see cref="RSSFeedReader.ViewModels.RenameChannelViewModel"/> class
        /// with a reference to a <see cref="RSSFeedReader.ViewModels.ChannelViewModel"/> instance.
        /// </summary>
        /// <param name="channelViewModel">The <see cref="RSSFeedReader.ViewModels.ChannelViewModel"/>
        /// instance to use as the data provider.</param>
        public RenameChannelViewModel(ChannelViewModel channelViewModel)
        {
            if (channelViewModel == null)
                throw new ArgumentNullException("channelViewModel");

            _channelViewModel = channelViewModel;
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the name of the folder.
        /// </summary>
        public string Title
        {
            get { return _channelViewModel.Title; }
        }
        #endregion

        #region Commands

        #region OKCommand
        /// <summary>
        /// Adds a new folder.
        /// </summary>
        public ICommand OKCommand
        {
            get
            {
                if (_OKCommand == null)
                    _OKCommand = new DelegateCommand<string>(DoOKCommand,
                        CanDoOKCommand);

                return _OKCommand;
            }
        }

        /// <summary>
        /// Determines if the OKCommand can be executed.
        /// </summary>
        /// <returns>True if the OKCommand can be executed, false otherwise.</returns>
        bool CanDoOKCommand(string title)
        {
            return !string.IsNullOrEmpty(title);
        }

        ///<summary>
        /// Executes the OKCommand.
        /// </summary>
        /// <param name="title">The new title of the channel.</param>
        void DoOKCommand(string title)
        {
            try
            {
                _channelViewModel.Title = title;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("An error has occured: {0}", ex.Message));
            }
            finally
            {
                OnRequestClose();
            }
        }
        #endregion

        #endregion
    }
}
